package com.tianbo.controller;

import com.tianbo.common.ModelAndPage;
import com.tianbo.model.TOlapDataset;
import com.tianbo.model.TOlapXmldoc;
import com.tianbo.service.OlapDataSetService;
import com.tianbo.service.OlapXmldocService;
import com.tianbo.service.OlapXmlnodeService;
import com.tianbo.util.dao.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;

/**
 * Created by Mr.Xu on 2017/9/14.
 */
@Controller
@RequestMapping("/olapxmldoc")
public class OlapXmldocController {
    @Autowired
    private OlapXmldocService olapXmldocService;
    @Autowired
    private OlapDataSetService olapDataSetService;
    @Autowired
    private OlapXmlnodeService olapXmlnodeService;
    @RequestMapping("showlist")
    public ModelAndView findXmldoc(@ModelAttribute Page page)throws Exception{
        int pagelenth=10;
        int start=0;
        if(page.getBegin()>=0){start=page.getBegin();}
        if(page.getLength()>0){pagelenth=page.getLength();}
        ModelAndView modelAndView=new ModelAndView();
        ModelAndPage modelAndPage=new ModelAndPage();
        modelAndPage=olapXmldocService.findXmldoc(start,pagelenth);
        List<TOlapDataset> olapDatasetList=olapDataSetService.findOlapDataSet();
        modelAndView.addObject("modelAndPage",modelAndPage);
        modelAndView.addObject("olapDatasetList",olapDatasetList);
        modelAndView.setViewName("olapxmldoc/list");
        return modelAndView;
    }
    @RequestMapping("addxmldoc")
    public String addXmldoc(HttpServletRequest request,TOlapXmldoc olapXmldoc){
        String doctype=request.getParameter("doctype");
        String docname=request.getParameter("docname");
        String datasetid=request.getParameter("datasetid");
        String active=request.getParameter("active");
        olapXmldoc.setDoctype(doctype);
        olapXmldoc.setDocname(docname);
        olapXmldoc.setDatasetid(new BigDecimal(datasetid));
        olapXmldoc.setActive(active);
        olapXmldocService.addXmldoc(olapXmldoc);
        return "redirect:/olapxmldoc/showlist";
    }
    @RequestMapping("findByid")
    public ModelAndView findByid(HttpServletRequest request,BigDecimal docid) throws Exception{
        ModelAndView modelAndView=new ModelAndView();
        String id=request.getParameter("docid");
        BigDecimal bd=new BigDecimal(id);
        TOlapXmldoc olapXmldoc=olapXmldocService.findByid(bd);
        List<TOlapDataset> olapDatasetList=olapDataSetService.findOlapDataSet();
        modelAndView.addObject("olapDatasetList",olapDatasetList);
        modelAndView.addObject("olapXmldoc",olapXmldoc);
        modelAndView.setViewName("olapxmldoc/edit");
        return  modelAndView;
    }
    @RequestMapping("updateXmldoc")
    public String updateXmldoc(HttpServletRequest request,TOlapXmldoc olapXmldoc){
        String docid=request.getParameter("docid");
        String doctype=request.getParameter("doctype");
        String docname=request.getParameter("docname");
        String datasetid=request.getParameter("datasetid");
        String active=request.getParameter("active");
        olapXmldoc.setActive(active);
        olapXmldoc.setDatasetid(new BigDecimal(datasetid));
        olapXmldoc.setDocname(docname);
        olapXmldoc.setDoctype(doctype);
        olapXmldoc.setDocid(new BigDecimal(docid));
        olapXmldocService.updateXmldoc(olapXmldoc);
        return "redirect:/olapxmldoc/showlist";
    }
    @RequestMapping("deleteXmldoc")
    public String updateXmldoc(HttpServletRequest request,BigDecimal docid){
        String id=request.getParameter("docid");
        int result=olapXmlnodeService.deleteXmlnode(new BigDecimal(id));
        if(result>0){
            olapXmldocService.deleteXmldoc(new BigDecimal(id));
            return "redirect:/olapxmldoc/showlist";
        }else{
            return "500";
        }
    }
}
